import React, { Component } from 'react'
import Mysearch from '../../../common/search';
import { SearchWrap } from './style'
import History from '../../../common/history'
import localStorage from 'local-storage';
import AdListUl from "./compoments/likeListUl";
import LoadMore from '../../../common/loadMore'
import { getshoplikeList } from "../../../apis/shop";
import Sticky from '../../../common/Sticky'
class Search extends Component {
    constructor(props) {
        super(props);
        this.state = {
            keywrod: "",
            list: localStorage.get('shopUser') ? localStorage.get('shopUser').shopSerachHistory : []
        }
    }
    render() {
        var { keywrod, list } = this.state;
        return (
            <Sticky header={
                <Mysearch {...this.props}
                    keywrod={keywrod}
                    getSearchList={(val) => { this.getSearchList(val) }}
                    clear={this.clear.bind(this)}
                />}>
                {(state) => {
                    return <SearchWrap >
                        {keywrod === "" ?
                            <History
                                del={this.del.bind(this)}
                                getSearchList={(val) => { this.getSearchList(val) }}
                                list={list}
                            />
                            :
                            <LoadMore params={{ keywrod }} config={{ header: "搜索结果" }} ShowListUl={AdListUl} httpDatas={getshoplikeList} />
                        }
                    </SearchWrap>
                }}
            </Sticky>
        )
    }
    clear() {
        this.setState({ keywrod: "" })
    }
    getSearchList(val) {
        if (val === "") {
            val = "all"
        }
        let shopUser = localStorage.get('shopUser') || {};
        let arr = shopUser.shopSerachHistory || [];
        arr.push(val)
        shopUser.shopSerachHistory = [...new Set(arr)]
        localStorage.set('shopUser', shopUser)
        this.setState({ keywrod: val })
    }
    del() {
        let shopUser = localStorage.get('shopUser') || {};
        shopUser.shopSerachHistory = []
        localStorage.set('shopUser', shopUser)
        this.setState({ list: [] })
    }
}
export default Search;
